package top.lshaci.learning.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * NioFileChannelCopy
 *
 * @author lshaci
 * @since 1.0.0
 */
public class NioFileChannelCopy {

    public static void main(String[] args) throws Exception {
        String filename = "E:/NIO1.txt";
        FileInputStream fis = new FileInputStream(filename);

        final FileChannel channel = fis.getChannel();
        FileOutputStream fos = new FileOutputStream("E:/NIO_COPY2.txt");

        final FileChannel outChannel = fos.getChannel();

        final ByteBuffer byteBuffer = ByteBuffer.allocate(10);

        while (channel.read(byteBuffer) != -1) {
            byteBuffer.flip();
            outChannel.write(byteBuffer);
            byteBuffer.clear();
        }

        fis.close();
        fos.close();
    }
}
